package com.fengwuj.tkmybatisdemo.study.datastruct;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class MoveArrayNums {

    //数组移动
    public static void moveNums(int[] nums){
        int idx = 0;
        //遍历，提取有效值
        for (int num : nums) {
            if (num != 0) {
                nums[idx++] = num;
            }
            System.out.println(Arrays.toString(nums));
        }
        //idx之后置零
        while (idx < nums.length) {
            nums[idx++] = 0;
        }
        System.out.println(Arrays.toString(nums));
    }

    //找出数组中最长连续的1
    public static void findMaxConsecutiveOnes(int[] nums) {
        int max = 0, cur = 0;
        for (int x : nums) {
            cur = x == 0 ? 0 : cur + 1;
            max = Math.max(max, cur);
        }
        System.out.println(max);
    }

    //分治算法
    public static List<Integer> diffWaysToCompute(String input) {
        List<Integer> ways = new ArrayList<>();
        for (int i = 0; i < input.length(); i++) {
            char c = input.charAt(i);
            if (c == '+' || c == '-' || c == '*') {
                List<Integer> left = diffWaysToCompute(input.substring(0, i));
                List<Integer> right = diffWaysToCompute(input.substring(i + 1));
                for (int l : left) {
                    for (int r : right) {
                        switch (c) {
                            case '+':
                                ways.add(l + r);
                                break;
                            case '-':
                                ways.add(l - r);
                                break;
                            case '*':
                                ways.add(l * r);
                                break;
                        }
                    }
                }
            }
        }
        if (ways.size() == 0) {
            ways.add(Integer.valueOf(input));
        }
        System.out.println(Arrays.toString(ways.toArray()));
        return ways;
    }


    public static void main(String[] args) {
        diffWaysToCompute("2-1-1");
    }
}
